package com.zjw.zy.coreui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.constant.SpecialDay
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/13 11:01
 *
 * 日历自定义View
 *
 */
class CalendarItemView : BaseViewGroup {

    private val mDay: TextView by lazy { findViewById(R.id.calendar_month_of_day) }
    private val mChinaDay: TextView by lazy { findViewById(R.id.calendar_month_of_china_day) }
    private val mSpecialDay: TextView by lazy { findViewById(R.id.calendar_special) }
    private val mLine: View by lazy { findViewById(R.id.calendar_item_line) }
    private lateinit var data: TimeUtils.TimeBean

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.calendar_item_view
    }

    override fun onBindListener() {}
    override fun onBindValue() {}
    override fun onFindView(rootView: View) {}

    fun getData() = data

    fun bindData(timeBean: TimeUtils.TimeBean) {
        data = timeBean
        mDay.text = timeBean.day.toString()
        mChinaDay.text = if (timeBean.lunar.day == 1) {
            mLine.setBackgroundColor(ResourcesUtils.getColor(R.color.month_first_day_color))
            timeBean.lunar.chinaMonthString
        } else {
            mLine.setBackgroundColor(ResourcesUtils.getColor(R.color.transparent))
            timeBean.lunar.chinaDayString
        }

        setSpecialStyle(timeBean.special)
    }

    private fun setSpecialStyle(special: String) {
        mSpecialDay.text = special
        var color = ResourcesUtils.getColor(R.color.common_day_color)

        //周末
        if (data.dayOfWeek == 0 || data.dayOfWeek == 6) {
            color = ResourcesUtils.getColor(R.color.common_weekday_color)
        }

        if (special != SpecialDay.NULL) {
            color = ResourcesUtils.getColor(R.color.special_day_common_color)
        }
        mDay.setTextColor(color)
        mChinaDay.setTextColor(color)
        mSpecialDay.setTextColor(color)
    }
}